from collections.abc import Iterable

d = {'a': 1, 'b': 2, 'c': 3}

# 遍历key
for key in d:
    print(key)

# 遍历value
for value in d.values():
    print(value)

# 遍历key-value
for k, v in d.items():
    print(k, v)

# 判断是否可迭代
print(isinstance('abc', Iterable))
print(isinstance([1, 2, 3], Iterable))
print(isinstance(123, Iterable))

# enumerate函数可以把一个list变成索引-元素对
for i, value in enumerate(['A', 'B', 'C']):
    print(i, value)

# 同时引用了两个变量
for x, y in [(1, 1), (2, 4), (3, 9)]:
    print(x, y)


# 练习，使用迭代查找一个list中最小和最大值，并返回一个tuple

def findMinAndMax(L):
    if L:
        min = L[0]
        max = L[0]
        for i in L:
            if i < min:
                min = i
            if i > max:
                max = i
        return (min, max)
    return (None, None)


# 测试
if findMinAndMax([]) != (None, None):
    print('测试失败!')
elif findMinAndMax([7]) != (7, 7):
    print('测试失败!')
elif findMinAndMax([7, 1]) != (1, 7):
    print('测试失败!')
elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):
    print('测试失败!')
else:
    print('测试成功!')
